from flask import current_app
from utils.database import db
from .Problem import Problem

class ProblemDAO:
    @staticmethod
    def insert(problem):
        try:
            db.session.add(problem)
            db.session.commit()
            current_app.logger.info("Problem insert success!")
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(f"Problem insert error: {str(e)}")

    @staticmethod
    def delete(id):
        try:
            problem = Problem.query.get(id)
            if problem:
                db.session.delete(problem)
                db.session.commit()
                current_app.logger.info("Delete problem success")
            else:
                current_app.logger.info("Problem not found")
        except Exception as e:
            db.session.rollback()
            current_app.logger.error(f"Delete problem error: {str(e)}")

    @staticmethod
    def select_all():
        problems = Problem.query.all()
        return [problem.to_simple_dict() for problem in problems]

    @staticmethod
    def select_one(id):
        return Problem.query.get(id)

    # 分页查询
    @staticmethod
    def select_by_page_size(page, size):
        pass